package com.example.renyang.happysell;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.renyang.happysell.MyUntil.Car;
import com.example.renyang.happysell.MyUntil.Data;
import com.example.renyang.happysell.net.HttpUtil;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import infClass.Food;
import infClass.OrderInform;

public class fragment_shopcart extends Fragment {
    private List<String> maphelp;
    private List<Food> list;
    private ListView listView;
    private TextView all;
    private Button tjbt;
    private List<OrderInform> orderlist;
    List<Object> list1;
    Handler handler;
    String fname;
    String stprice;
    String f="";
    int many=0;
   private Myadopt mydopt;
    private Context context;
    public static Food food=new Food();

    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        View view= inflater.inflate(R.layout.fragment_shopcart, container, false);
        list= Car.getCarlist();
        listView=(ListView)view.findViewById(R.id.merlist);
        TextView tet=(TextView)view.findViewById(R.id.merchantname);
        all=(TextView)view.findViewById(R.id.many);
        tjbt=(Button)view.findViewById(R.id.xuanhaole);
 //       tjbt.setClickable(false);
        tjbt.getBackground().setAlpha(0);
        maphelp=new ArrayList<String>();
        for(String key:Car.getCarmap().keySet()){
            maphelp.add(key);
        }
        tet.setText(Data.getName() + "购物车");
        mydopt=new Myadopt();
        Button fanhui=(Button)view.findViewById(R.id.fanhui);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 //   Intent intent = new Intent(context,fragement_);
                  //startActivity(intent);
            }
        });
        add();
        new Thread() {
            public void run(){
                
            }
        }.start();
      //  listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            all.setText(stprice);
            }
        });
        tjbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                food.setFname(fname);
                food.setPrice(stprice);
                Log.i("zx", "fod" + fname);
                Data.setDataorderlist(orderlist);
                Intent intent=new Intent();
                intent.setClass(context,fragment_orderMan.class);
                intent.putExtra("name",fname);
                intent.putExtra("price",stprice);
                context.startActivity(intent);
                Toast.makeText(context.getApplicationContext(),"提价购物车成功",Toast.LENGTH_SHORT).show();

            }
        });

        return view;
    }//end oncreat

    private void add() {
            ArrayList<HashMap<String,Object>> listItem = new ArrayList<HashMap<String,Object>>();
/*在数组中存放数据*/
            for(int i=0;i<1;i++)
            {
                HashMap<String, Object> map = new HashMap<String, Object>();
                // map.put("Image",tupian);//加入图片
                //map.put("Title", ":"+foods[i].getFname());
                // map.put("Title1", "");
                //map.put("Title2",":"+ foods[i].getprice());

                map.put("Image",R.drawable.kit);//加入图片
                map.put("Title","炒米粉" );
                map.put("Title1", "五颗星");
                map.put("Title2", "33");
                listItem.add(map);


            }

            SimpleAdapter mSimpleAdapter = new SimpleAdapter(context,listItem,//需要绑定的数据
                    R.layout.mlist,//每一行的布局
//动态数组中的数据源的键对应到定义布局的View中
                    new String[] {"Image","Title", "Title1","Title2"},
                    new int[] {R.id.foodimg,R.id.foodname,R.id.foodevaluate,R.id.foodpice}
            );
            listView.setAdapter(mydopt);//为ListView绑定适配器
        }
    String[] a=new String[]{"1","2","3","4","5","6"};



    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }

    private class Myadopt extends BaseAdapter {

        @Override
        public int getCount() {
            return Car.carmap.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = View.inflate(context, R.layout.mlist, null);
            final TextView foodname = (TextView) view.findViewById(R.id.foodname);
            final TextView foodpice = (TextView) view.findViewById(R.id.foodpice);
            ImageView xuanzhong = (ImageView) view.findViewById(R.id.xuanzhong);
            xuanzhong.setImageResource(R.drawable.kit);
            String name=maphelp.get(position);
            String num=Car.getCarmap().get(name);
            foodname.setText(name);
                fname=name;
                String pircel=searchfood(name);
                stprice=pircel;
                Log.i("zx","KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK2"+pircel);
            try {
                String countprice = String.valueOf(Integer.parseInt(pircel) * Integer.parseInt(num));
                Log.i("zx","KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK2"+countprice);
                //      String m = String.valueOf(position + 1);
                foodpice.setText(countprice);
            }catch(Exception e){
                e.printStackTrace();
                Log.i("zx","设置购物车出错");
            }
            xuanzhong.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("zx","*********************3"+stprice);
                    fname = foodname.getText().toString();
                    f += fname;
                    stprice = foodpice.getText().toString();
                    many += Integer.parseInt(stprice);
                    String s = String.valueOf(many);
                    all.setText(s);
                    int money = 15 - many;


                    if (money > 0) {
                        tjbt.setText("还差" + money + "元");
                    } else {
                        tjbt.setText("选好了");
                    }


                }
            });
            return view;
        }

        private String searchfood(String name) {
            Log.i("zx","KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK1"+name);
            String foodprice=null;
            for(Food food:Car.getCarlist()){
               if(food.getFname().equals(name)) {
                   Log.i("zx","KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK4"+food.getPrice());

                  foodprice=food.getPrice();

               }
            }
            Log.i("zx","KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK4"+foodprice);
          return foodprice;
        }
    }
}
